﻿@page "/bbs/post/{id}"
@inherits BaseComponent

<div class="container cl-container cl-post">
    <div>
        <CmsCard>
            <HeadTemplate>
                <CmsTitle Icon="far fa-file" Title="问题详情" />
                <UPostCount Post="model" />
            </HeadTemplate>
            <ChildContent>
                <div class="title">@model?.Title</div>
                @CmsUtils.GetMarkdownHtml(model?.Content)
                <div class="button">
                    <AntButton Name="返回" Type="default" OnClick="location='./bbs';" />
                </div>
            </ChildContent>
        </CmsCard>
        <div style="height:20px;"></div>
        <CmsCard Title="回复列表">
            @if (CurrentUser != null)
            {
                <div style="display:grid;grid-template-columns:40px auto;">
                    <div>
                        <AntAvatar Src="@CurrentUser?.AvatarUrl" />
                    </div>
                    <div>
                        @if (!string.IsNullOrWhiteSpace(errorMessage))
                        {
                            <AntAlert Type="error">@errorMessage</AntAlert>
                        }
                        <AntForm Id="@formId">
                            <CmsMarkdown Id="@nameof(ReplyFormInfo.Content)" AutoFocus="autoFocus" Height="200" Placeholder="在这里输入回答内容" />
                            <div class="button">
                                <AntButton Name="回复" OnClick="@($"return AntBlazor.validate('{formId}');")" />
                            </div>
                        </AntForm>
                    </div>
                </div>
            }
            @if (model?.Replies == null || model?.Replies.Count == 0)
            {
                <AntEmpty />
            }
            else
            {
                foreach (var item in model?.Replies)
                {
                    <div class="cl-reply">
                        <div class="author">
                            <AntAvatar Src="@item.Author?.AvatarUrl" />
                            <span>@item.Author?.Name</span>
                        </div>
                        <div class="content">@CmsUtils.GetMarkdownHtml(item.Content)</div>
                        <div class="counts">
                            <span class="time">@item.ReplyTime.ToString("yyyy-MM-dd HH:mm")</span>
                            <span style="margin:0 5px;">|</span>
                            <span class="like"><i class="far fa-thumbs-up" /> @item.LikeQty</span>
                        </div>
                    </div>
                }
            }
        </CmsCard>
    </div>
    <div>
        <SigninBox />
        <div style="height:20px;"></div>
        <UPostRank Service="Service" />
    </div>
</div>

@code {
    private ISiteService Service;
    private PostDetailInfo model;
    private bool autoFocus = false;
    private string formId = "reply-form";
    private string errorMessage = "";

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        Service = await CreateServiceAsync<ISiteService>();
        if (Context.Request.IsHandler(formId))
        {
            autoFocus = true;
            var model = Context.Request.GetModel<ReplyFormInfo>();
            model.BizId = Id;
            var result = await Service.SaveReplyAsync(model);
            if (!result.IsValid)
                errorMessage = result.Message;
        }
        model = await Service.GetPostAsync(ContentType.Interact, Id);
    }
}